package com.zhuiyun.project.api.venuemanagement.service.impl;


import com.zhuiyun.project.api.venuemanagement.entity.BanRecord;
import com.zhuiyun.project.api.venuemanagement.entity.SysUser;
import com.zhuiyun.project.api.venuemanagement.entity.VenueManagement;
import com.zhuiyun.project.api.venuemanagement.mapper.VenueManagementMapper;
import com.zhuiyun.project.api.venuemanagement.model.SelectSubordinateModel;
import com.zhuiyun.project.api.venuemanagement.model.VenueSelectModel;
import com.zhuiyun.project.api.venuemanagement.model.VenueUpdateModel;
import com.zhuiyun.project.api.venuemanagement.service.VenueManagementService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.JavaMailUntil;
import com.zhuiyun.project.utils.UploadFileUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.Message.RecipientType;

import javax.mail.Session;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.zhuiyun.project.utils.SecurityUtils.getLoginName;

/**
 * @ClassName CoachManagementServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/11/2 13:39
 **/
@Service
public class VenueManagementServiceImpl implements VenueManagementService {
    @Autowired
    private VenueManagementMapper venueManagementMapper;


    /*
    * @Author wuwan
    * @Description 修改场馆信息
    * @Date 2023/11/3 14:12
    * @Return
    */
    @Override
    @Transactional
    public CommonResult updateVenue(VenueUpdateModel venueUpdateModel, MultipartFile file) throws CommonException {
        if (ValidatorUtil.ValidationObject(venueUpdateModel)){
            SysUser tenantUser = new SysUser();
            if (file != null) {
                String s = UploadFileUtils.uploadFile(file);
                tenantUser.setAvatarId(Integer.parseInt(s));
            }
            // 修改账号表数据
            tenantUser.setId(venueUpdateModel.getId());
            tenantUser.setUserName(venueUpdateModel.getUserName());
            tenantUser.setPhone(venueUpdateModel.getPhone());
            tenantUser.setEmail(venueUpdateModel.getEmail());
            tenantUser.setUpdateTime(new Date());
            Integer user = venueManagementMapper.updateSysUser(tenantUser);
            // 修改场馆表数据
            VenueManagement venueManagement = new VenueManagement();
            // addModel 对象拷贝成 sysUser
            BeanUtils.copy(venueUpdateModel, venueManagement);
            SelectSubordinateModel selectSubordinateModel = venueManagementMapper.selectDirectorIdcard(venueUpdateModel.getId());
            venueManagement.setVenueUniqueIdentifier(selectSubordinateModel.getVenueUniqueIdentifier());
            Integer integer = venueManagementMapper.updateVenue(venueManagement);
            if (integer >= 1) {
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
    /*
    * @Author wuwan
    * @Description 场馆信息分页查询
    * @Date 2023/11/3 14:12
    * @Return
    */
    @Override
    public CommonResult selectVenue(PageRequest pageRequest,String userName,String phone) {
        MybatisPageHelper.startPage(pageRequest);
        List<VenueSelectModel> venueManagements = venueManagementMapper.selectVenue(userName, phone);
//        for (int i = 0; i < venueManagements.size(); i++) {
//            VenueSelectModel venueSelectModel = venueManagements.get(i);
//            String venueUniqueIdentifier = venueSelectModel.getVenueUniqueIdentifier();
//            VenueSelectModel venueSelectModel1 = venueManagementMapper.selectAccountCount(venueUniqueIdentifier);
//            Integer venueAccountCount = venueSelectModel1.getVenueAccountCount();
//            VenueSelectModel venueSelectModel2 = venueManagementMapper.selectUserCount(venueUniqueIdentifier);
//            Integer venueUserCount = venueSelectModel2.getVenueUserCount();
//            VenueSelectModel venueSelectModel3 = venueManagementMapper.selectCoachCount(venueUniqueIdentifier);
//            Integer venueCoachCount = venueSelectModel3.getVenueCoachCount();
//            venueSelectModel.setVenueUserCount(venueUserCount);
//            venueSelectModel.setVenueCoachCount(venueCoachCount);
//            venueSelectModel.setVenueAccountCount(venueAccountCount);
//        }
        PageResult pageResult = MybatisPageHelper.getPageResult(venueManagements);
        return CommonResult.ok(pageResult);
    }
//    /*
//    * @Author wuwan
//    * @Description 场馆封号
//    * @Date 2023/11/6 9:48
//    * @Return
//    */
    @Override
    @Transactional
    public CommonResult changeUserStatus(BanRecord banRecord) throws ParseException, MessagingException {
        // 向用户封禁记录表中存储数据
        venueManagementMapper.addBanRecord(banRecord);
        // 修改用户表的解封时间
        venueManagementMapper.addByTime(banRecord);
        SelectSubordinateModel selectsubordinate = venueManagementMapper.selectDirectorIdcard(banRecord.getId());
        List<SelectSubordinateModel> selectsubordinate1 = venueManagementMapper.selectsubordinate(selectsubordinate.getVenueUniqueIdentifier());
        for (int i = 0; i < selectsubordinate1.size(); i++) {
            SelectSubordinateModel selectSubordinateModel = selectsubordinate1.get(i);
            banRecord.setId(selectSubordinateModel.getId());
            venueManagementMapper.changeUserStatus(banRecord);
        }
        //	创建Session会话
        Session session = JavaMailUntil.createSession();
        //	创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 主题
        message.setSubject("健身房账号封禁");
        String banReason = banRecord.getBanReason();
        Date banTime = banRecord.getBanTime();
        // 设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 格式转化为字符串
        String formattedDate = sdf.format(banTime);
        String s = "封禁原因"+"："+banReason+"\n解封日期："+formattedDate+"\n如有疑惑请联系管理员:"+"15030698053";
        // 文本内容
        message.setText(s);
        // 发送的邮箱
        message.setFrom(new InternetAddress("3228627120@qq.com"));
        // 接受的邮箱
        message.setRecipient(RecipientType.TO, new InternetAddress(banRecord.getEmail()));
        System.out.println(message);
        //	发送
        Transport.send(message);
        return CommonResult.ok("封禁成功");
    }
    /*
    * @Author wuwan
    * @Description 根据id查询用户信息
    * @Date 2023/12/6 10:01
    * @Return
    */
    @Override
    public CommonResult selectById(PageRequest pageRequest, String id) {
        return CommonResult.ok(venueManagementMapper.selectById(pageRequest,id));
    }
    /*
    * @Author wuwan
    * @Description 用于计算解封日期
    * @Date 2023/12/6 16:50
    * @Return
    */
    public Date BanTimes(Integer banDate) throws ParseException {
        // 设置 banDate 的值为 365
        // 创建一个 Calendar 对象，并将其设置为当前时间
        Calendar calendar = Calendar.getInstance();
        // 将 Calendar 对象的日期加上 banDate 天
        calendar.add(Calendar.DAY_OF_YEAR, banDate);
        // 获取计算后的日期
        Date resultDate = calendar.getTime();
        // 格式化日期为精确到年月日时分秒的形式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(resultDate);
        // 创建日期格式化对象
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdfs.parse(formattedDate);
        return date;
    }
}

